package com.lej.app_interact.person

import androidx.lifecycle.MutableLiveData
import com.lej.app_interact.HAS_FALLOW
import com.lej.app_interact.InteractApiService
import com.lej.app_interact.NOT_FOLLOW
import com.lej.core.base.BaseModel
import com.lej.core.base.BaseViewModel
import com.lej.core.utils.extention.toast
import com.lej.dataservice.club.bean.interact.CreateFollowReq
import com.lej.dataservice.club.bean.interact.CreatorInfoReq
import com.lej.dataservice.club.bean.interact.CreatorSearchInfo
import com.lej.dataservice.club.http.base.toBody
import com.lej.dataservice.club.http.checkData

class InteractPersonViewModel : BaseViewModel<BaseModel>() {
    val isFollow: MutableLiveData<Int> = MutableLiveData(HAS_FALLOW)
    val creatorSearchInfo: MutableLiveData<CreatorSearchInfo> = MutableLiveData()

    suspend fun getCreatorInfo(id: String): CreatorSearchInfo? {
        kotlin.runCatching {
            val creatorInfoReq = CreatorInfoReq(id)
            return InteractApiService.instance.getCreatorInto(creatorInfoReq.toBody()).checkData()
        }.onFailure {
            it.message.toast()
        }
        return null
    }

    suspend fun followCreate(id: Int): Boolean {
        kotlin.runCatching {
            val creatorInfoReq = CreateFollowReq(id, NOT_FOLLOW)
            InteractApiService.instance.followCreator(creatorInfoReq.toBody()).checkData()
            return true
        }.onFailure {
            it.message.toast()
        }
        return false
    }
}